﻿using System;
using Microsoft.SharePoint;
using SPContrib.Extensions;

namespace SPContrib.SharePoint.Extensions
{
    /// <summary>
    /// Extensions for the String class
    /// </summary>
    public static class StringExtensions
    {
        static StringExtensions()
        {
            TypeMapper.RegisterMappingBetween<string, SPContentTypeId>(s =>
                {
                    try
                    {
                        return new SPContentTypeId(s);
                    }
                    catch (Exception e)
                    {
                        throw "StringExtensions: failed to create an instance of the SPContentTypeId class from input '{0}'"
                                .AsException(e, s);
                    }
                });
        }

        /// <summary>
        /// Returns an instance of SPContentTypeId that represents the string 
        /// </summary>
        public static SPContentTypeId AsContentTypeId(this string instance)
        {
            return TypeMapper.Resolve<string, SPContentTypeId>(instance);
        }
    }
}
